import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hsh/fam/assets.dart';

class FAvatarWidget extends StatelessWidget {
  final String? url;
  final double size;
  final double borderWidth;
  final Color borderColor;
  final double? radius;
  final String defaultAvatar;

  const FAvatarWidget({
    super.key,
    this.url,
    this.size = 60,
    this.borderWidth = 0,
    this.borderColor = Colors.white,
    this.radius,
    this.defaultAvatar = Assets.defaultAvatar,
  });

  @override
  Widget build(BuildContext context) {
    double avatarSize = size - borderWidth * 2;
    ImageProvider imageProvider = url != null && url!.isNotEmpty ? CachedNetworkImageProvider(url!) : AssetImage(defaultAvatar) as ImageProvider;
    return Container(
      padding: EdgeInsets.all(borderWidth),
      decoration: BoxDecoration(color: borderColor, borderRadius: BorderRadius.circular(radius ?? size / 2)),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(radius ?? avatarSize / 2),
        child: Image(
            image: imageProvider,
            width: avatarSize,
            height: avatarSize,
            fit: BoxFit.cover,
            errorBuilder: (context, error, stackTrace) => Image.asset(defaultAvatar, width: avatarSize, height: avatarSize, fit: BoxFit.cover)),
      ),
    );
  }
}
